package com.tjhk.netty.model;

import com.tjhk.netty.serialize.BasicFieldSerializeUtil;
import io.netty.buffer.ByteBuf;

public abstract class AbstractMessage<T> implements Message{

    private int messageId = -1;

    @Override
    public Integer getMessageId() {
        return this.messageId;
    }

    public void setMessageId(int messageId) {
        this.messageId = messageId;
    }

    /**
     * ByteBuf -> Message
     * @param bodyBuf
     */
    @Override
    public void readFrom(ByteBuf bodyBuf){
        this.setPayload(bodyBuf.slice());
        BasicFieldSerializeUtil.readFrom(bodyBuf,this);
    }

    /**
     * Message -> ByteBuf
     * @return
     */
    @Override
    public ByteBuf writeTo(){
        return BasicFieldSerializeUtil.writeTo(this);
    }

    /**
     * 获取消息头
     * @return
     */
    public abstract T getHeader();

    public abstract void setPayload(ByteBuf bodyBuf);
}
